package com.runlooker.runer.entity;

import java.util.Iterator;

import net.tsz.afinal.annotation.sqlite.Id;
import net.tsz.afinal.annotation.sqlite.Table;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterGroup;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.ItemStatus;

import com.runlooker.runer.db.RunerTableSql;
import com.runlooker.runer.util.JidUtil;
import com.runlooker.runer.util.L;

/**
 * 联系人信息
 * 
 * @author YINENG
 *
 */
@Table(name = RunerTableSql.ROSTER_ENTRY_TABLE)
public class LocalRosterEntity {

	// none:
	// 该用户没有订阅一该联系人的出席信息, 并且联系人也没有订阅该用户的出席信息; 这是缺省值,
	// 所以如果没有subscription属性，那么该状态被理解为"none"
	// to:
	// 该用户订阅了该联系人的出席信息, 但是联系人没有订阅用户的出席信息
	// from:
	// 该联系人订阅了该用户的出席信息, 但是该用户没有订阅联系人的出席信息
	// both:
	// 该用户和该联系人互相订阅了对方的出席信息(也称为"相互订阅")
	//
	// 'subscription'属性的值可以包括 "remove", 它表示该条目已经从名册中移除了; 在一个 roster set 中
	// ，该服务器必须忽略除了"remove"之外'subscription'属性的所有值.
	public static final int SUBSCRIBE = 1;
	public static final int UNSUBSCRIBE = 2;

	private static String tag = LocalRosterEntity.class.getSimpleName();

	public static final int NONE = 1;
	public static final int FROM = 2;
	public static final int BOTH = 3;
	public static final int REMOVE = 4;
	public static final int TO = 5;
	@Id(column = "account")
	private String account;

	private String name;
	// 对应Constant文件中的RosterPacket.ItemStatus: subscribe = 0,unsubscribe = 1;
	/**
	 * 值（SUBSCRIBE(请求订阅)、UNSUBSCRIBE(请求退订)，表示发送申请时（添加、 删除），对方没在线，对方上线后，会返回回执
	 */
	private int status;
	// 对应Constant文件中的RosterPacket.ItemType: both = 0,from = 1,none =2,remove =
	// 3,to = 4
	/**
	 * 值(BOTH（互为关注）、FROM（别人关注你）、NONE（互相不关注）、REMOVE（不想获得别人的presence，拉入黑名单
	 * ）、TO（你关注别人）)
	 */
	private int itemType;

	// 存储联系人所在的所有组（可能有多个组），组名由"[@group]"区分，如：成都群[@group]四川群[@group]北京群[@group@]
	private String groupsName;

	public static LocalRosterEntity entryToEntity(RosterEntry rosterEntry) {
		LocalRosterEntity entity = new LocalRosterEntity();
		entity.account = JidUtil.getAccountFromJID(rosterEntry.getUser());
		Iterator<RosterGroup> groups = rosterEntry.getGroups().iterator();
		StringBuilder groupsnameBuilder = new StringBuilder();
		while (groups.hasNext()) {
			groupsnameBuilder.append(groups.next().toString());
			groupsnameBuilder.append("[group]");
		}
		entity.setGroupsName(groupsnameBuilder.toString());
		entity.setName(rosterEntry.getName());

		// the status is RosterPacket.ItemStatus.SUBSCRIPTION_PENDING,
		// the contact has to answer the subscription request.
		ItemStatus status = rosterEntry.getStatus();
		if (status != null && status.equals(ItemStatus.SUBSCRIPTION_PENDING)) {
			entity.status = SUBSCRIBE;
		} else if (status != null
				&& status.equals(ItemStatus.UNSUBSCRIPTION_PENDING)) {
			entity.status = UNSUBSCRIBE;
		}

		if (rosterEntry.getType().equals(RosterPacket.ItemType.both)) {
			L.i(tag, "both");
			entity.itemType = BOTH;
		} else if (rosterEntry.getType().equals(RosterPacket.ItemType.from)) {
			L.i(tag, "from");
			entity.itemType = FROM;
		} else if (rosterEntry.getType().equals(RosterPacket.ItemType.none)) {
			L.i(tag, "NONE");
			entity.itemType = NONE;
		} else if (rosterEntry.getType().equals(RosterPacket.ItemType.remove)) {
			L.i(tag, "REMOVE");
			entity.itemType = REMOVE;
		} else if (rosterEntry.getType().equals(RosterPacket.ItemType.to)) {
			L.i(tag, "TO");
			entity.itemType = TO;
		}
		return entity;
	}

	@Override
	public String toString() {
		return "account:  " + account + "     status:   " + status
				+ "  itemType:  " + itemType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGroupsName() {
		return groupsName;
	}

	public void setGroupsName(String groupsName) {
		this.groupsName = groupsName;
	}

	public int getItemType() {
		return itemType;
	}

	public void setItemType(int itemType) {
		this.itemType = itemType;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

}
